﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class KyluatDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Kyluat");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Kyluat {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }



        public bool Insert(object obj)
        {
            var o = (Kyluat)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Kyluat '{0}', N'{1}', N'{2}', '{3}' ";
            sql = string.Format(sql, o.STT_QDKL, o.LYDO_KL, o.NOIRA_QDKL, o.NGAYRA_QDKL.ToString("dd/MM/yyyy"));
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Kyluat)obj;
            var sql = "set dateformat dmy; execute sp_Update_Kyluat '{0}', N'{1}', N'{2}', '{3}' ";
            sql = string.Format(sql, o.STT_QDKL, o.LYDO_KL, o.NOIRA_QDKL, o.NGAYRA_QDKL.ToString("dd/MM/yyyy"));
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select * from tb_Kyluat";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search_Sokyluat(string name)
        {
            var sql = @"set dateformat dmy; execute sp_Search_Sokyluat '" + name + "' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search_Lydokyluat(string name)
        {
            var sql = @"Select STT_QDKL, LYDO_KL, NOIRA_QDKL, NGAYRA_QDKL from tb_Kyluat Where LYDO_KL like N'%" + name + "%'  ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Kyluat o)
        {
            string sql;
            if (o.STT_QDKL != "")
                sql = @"Select * from tb_Kyluat Where STT_QDKL like '%" + o.STT_QDKL + "%'  ";
            else if (o.LYDO_KL !="")
                sql = @"Select * from tb_Kyluat Where LYDO_KL like N'%" + o.LYDO_KL + "%' ";
            else 
                sql = @"Select * from tb_Kyluat NOIRA_QDKL like N'%" + o.NOIRA_QDKL + "%' ";

            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(STT_QDKL) as Ma From tb_Kyluat";
            return ExecuteQuery(sql);
        }

        #endregion
    }
}
